package config

import (
	"errors"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
)

type Rpc struct {
	AnalyzerAddr string `yaml:"analyzer_addr"` // e.g. localhost:50051
	Id           string `yaml:"id"`            // id/name of tracker
	AuthKey      string `yaml:"auth_key"`      // authentication key needed to access Analyzer
}

type Config struct {
	RpcConf Rpc `yaml:"rpc"`
}

func (c *Config) Parse(path string) (*Config, error) {
	configFile, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	// check file existence
	if _, existErr := os.Stat(path); existErr != nil && os.IsNotExist(existErr) {
		return nil, errors.New("config file does not exist at " + path)
	}
	// parse file
	err = yaml.Unmarshal(configFile, c)
	if err != nil {
		return nil, errors.New("config file cannot be parsed, check your syntax ")
	}
	return c, err
}

func (c *Config) Validate() error {
	return nil
}
